﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;

namespace ETRoboConTrakingSystem
{
    /// <summary>
    /// Interaction logic for TimeTrackingSystemTopWindow.xaml
    /// </summary>
    [Export]
    public partial class TimeTrackingSystemTopWindow : Window
    {
        private readonly string extensionDir = "ExtModules";
 //       [Import(AllowRecomposition=true)]
   //     private ManagementWindowFactory managementWindowFactory { get; set; }
        [ImportMany(typeof(ISwitchBoxDevice))]
        private Lazy<ISwitchBoxDevice,IDictionary<string,object>>[] switchBoxDevices { get; set; }

        [Import(typeof(ISwitchBoxClient))]
        private Lazy<ISwitchBoxClient> gameManagementWindow { get; set; }

        public TimeTrackingSystemTopWindow()
        {
            InitializeComponent();

            if (!Compose())
            {
                this.Close();
            }
            foreach (var export in this.switchBoxDevices)
            {
                lbSwitchBoxSelect.Items.Add(export.Metadata["DeviceType"] as string);
            }
        }

        public void OnImportsSatisfied()
        {
        }

        private CompositionContainer myContainer;
        private CompositionContainer GetContainerFromDirectry()
        {
            var catalog = new AggregateCatalog();
            var thisAssembly = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
            catalog.Catalogs.Add(thisAssembly);
            catalog.Catalogs.Add(new DirectoryCatalog(extensionDir));

            var container = new CompositionContainer(catalog);

            return container;
        }

        private bool Compose()
        {
            myContainer = GetContainerFromDirectry();
            try
            {
                myContainer.ComposeParts(this);
                AppDomain.CurrentDomain.SetData("CurrentCompositionContainer", myContainer);
            }
            catch (CompositionException ex)
            {
                foreach (var error in ex.Errors)
                {
                    MessageBox.Show(error.Description);
                }
                MessageBox.Show(ex.ToString());
                return false;
            }
            return true;
        }

        private void buttonShowManagementWindow_Click(object sender, RoutedEventArgs e)
        {
            if (currentDevice != null)
            {
                var client = gameManagementWindow.Value;
                currentDevice.Start(client);
                var mgmtWindow = client as Window;
                mgmtWindow.Show();
            }

     //       var mgmtWindow = managementWindowFactory.GetWindow(
        }

        private void lbSwitchBoxSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }

        private ISwitchBoxDevice currentDevice = null;

        private void buttonLaunchSB_Click(object sender, RoutedEventArgs e)
        {
            if (lbSwitchBoxSelect.SelectedItem != null)
            {
                foreach (var sb in switchBoxDevices)
                {
                    if (sb.Metadata["DeviceType"].ToString().CompareTo(lbSwitchBoxSelect.SelectedItem.ToString()) == 0)
                    {
                        Window sbWindow = sb.Value as Window;
                        sbWindow.Show();
                        currentDevice = sb.Value;
//                        buttonShowGameManagementWindow.IsEnabled = true;
                        buttonShowManagementWindow.IsEnabled = true;
                        break;
                    }
                }
            }
        }

        private void buttonShowGameManagementWindow_Click(object sender, RoutedEventArgs e)
        {
            var window = new GameConfiguration();
            window.Show();
        }
    }
}
